package 算法.线性表;

import java.util.Arrays;

public class 寻找最小重复的数 {
    public static void main(String[] args) {
        int [] array = {1,0,2,3,5,4};
        System.out.println(Arrays.toString(array));
        System.out.println(cheak(array));
    }

    public static int cheak(int[] array){
        for (int i = 0; i < array.length; i++) {
            // 1.判断a[i]元素是否合法
            if (array[i]>=array.length || array[array[i]]>= array.length)
                return -1;
            while (array[i]!=i && array[array[i]]!=array[i]){
                if (array[i]>=array.length || array[array[i]]>= array.length)
                    return -1;
                int temp = array[i];
                array[i] = array[array[i]];
                array[temp]= temp;
                System.out.println(Arrays.toString(array));
            }
            if (array[i]!=i && array[array[i]]==array[i])
                return array[i];
        }
        return -1;
    }
}
